package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.pojo.TPerson;
import com.dk.service.TpersonService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {
    @Reference//用这个注解来远程调用service而不是Autowired
    private TpersonService tpersonService;

    @RequestMapping("toShow")
    public String toShow(){
        return "login";
    }
    @RequestMapping("deleteLogin")
    public void deleteLogin(){};

    @RequestMapping("unauthor")
    public String unauthor(){
        return "user/unauthor";
    };
    @RequestMapping("toIndex")
    public String toIndex(){
        return "index";
    }
    @RequestMapping("getInfo")
    @ResponseBody
    public Map getinfo(String pname, String pwd,  Model model){
        Map map = new HashMap();
        //获取subject对象
        Subject subject = SecurityUtils.getSubject();
        //将我们的账号密码装成subject
        UsernamePasswordToken token = new UsernamePasswordToken(pname,pwd);
        //调用subject方法来执行登录逻辑
        try {
            subject.login(token);

        }catch (UnknownAccountException e1){
            map.put("code",1);
            map.put("msg","账号不存在");
            return map;
        }catch (IncorrectCredentialsException e2){
            System.out.println(e2);
            map.put("code",2);
            map.put("msg","密码错误");
            return map;
        }
        map.put("code",0);
        map.put("msg","登陆成功");
        return map;
    }
}
